package com.chidopi.member.service.impl;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.chidopi.member.dao.IMemberDao;
import com.chidopi.member.domain.Book2City;
import com.chidopi.member.domain.BookCities;
import com.chidopi.member.domain.BookItems;
import com.chidopi.member.domain.Member2Book;
import com.chidopi.member.domain.Member2Device;
import com.chidopi.member.domain.Member2Subscribe;
import com.chidopi.member.domain.Member2SubscribeHis;
import com.chidopi.member.domain.Members;
import com.chidopi.member.domain.Subscriptions;
import com.chidopi.member.service.IMemberService;

public class MemberServiceImpl implements IMemberService {
	private static Logger logger = LoggerFactory.getLogger(MemberServiceImpl.class);
	@Autowired
	@Qualifier("memberDao")
	private IMemberDao dao;
			
	
	@Override
	public int insertMember2Device(Member2Device mem2dev) {
		return this.dao.insertMember2Device(mem2dev);
	}

	@Override
	public int updateMember2Device(Member2Device mem2dev) {
		return this.dao.updateMember2Device(mem2dev);
	}

	@Override
	public List<Member2Device> findMember2Device(String email, int city_id) {
		return this.dao.findMember2Device(email, city_id);
	}

	
	@Override
	public int checkMember2Device(String email, int city_id, String device,
			String genkey) {
		return this.dao.checkMember2Device(email, city_id, device, genkey);
	}

	@Override
	public BookCities findBookCitiesById(int city_id) {
		return this.dao.findBookCitiesById(city_id);
	}

	@Override
	public BookItems findBookItemsById(int book_id) {
		return this.dao.findBookItemsById(book_id);
	}

	@Override
	public Member2Device findMember2DeviceById(String email, int city_id,
			String device) {
		return this.dao.findMember2DeviceById(email, city_id, device);
	}

	@Override
	public Book2City findBook2CityByPK(int city_id, int book_id) {
		return this.dao.findBook2CityByPK(city_id, book_id);
	}

	@Override
	public int validateMember(String email, int city_id, String pwd) {
		return this.dao.validateMember(email, city_id, pwd);
	}

	@Override
	public Members getMember(String email, int city_id) {
		return this.dao.getMember(email, city_id);
	}

	@Override
	public int updateMember(Members member) {
		return this.dao.updateMember(member);
	}

	@Override
	public List<Member2Subscribe> findMember2Subscribe(String email, int city_id) {
		return this.dao.findMember2Subscribe(email, city_id);
	}

	@Override
	public List<Member2Subscribe> findMember2SubscribeByNoStart(String email,
			int city_id) {
		return this.dao.findMember2SubscribeByNoStart(email, city_id);
	}

	@Override
	public int insertMember2Subscribe(Member2Subscribe mem2sub) {
		return this.dao.insertMember2Subscribe(mem2sub);
	}

	@Override
	public int insertMember2SubscribeHis(Member2SubscribeHis mem2subHis) {
		try {
			return this.dao.insertMember2SubscribeHis(mem2subHis);
		} catch (Exception e) {
			logger.warn("insertMember2SubscribeHis:", e);
			return 0;
		}
	}

	@Override
	public int updateMember2Subscribe(Member2Subscribe mem2sub) {
		return this.dao.updateMember2Subscribe(mem2sub);
	}

	@Override
	public Member2Subscribe checkMember2Subscribe(String email, int city_id) {
		return this.dao.checkMember2Subscribe(email, city_id);
	}

	@Override
	public Subscriptions findSubscriptionById(int id) {
		return this.dao.findSubscriptionById(id);
	}

	@Override
	public int insertMember2Book(Member2Device mem2dev, int book_id) {
		Member2Book mem2bok = new Member2Book(mem2dev);
		mem2bok.setBook_id(book_id);
		return this.dao.insertMember2Book(mem2bok);
	}

	@Override
	public int updateMember2Book(Member2Device mem2dev, String json) {
		JSONArray ary = JSONArray.fromObject(json);
		for (int i=0, n=ary.size(); i<n; i++) {
			Member2Book mem2bok = new Member2Book(mem2dev);
			JSONObject obj = ary.getJSONObject(i);
			mem2bok.setBook_id(obj.getInt("id"));
			mem2bok.setGenkey(obj.getString("key"));
			this.dao.updateMember2Book(mem2bok);
		}
		return ary.size();
	}

	@Override
	public List<Member2Book> findMember2Book(Member2Device mem2dev) {
		return this.dao.findMember2Book(mem2dev);
	}
}
